package com.ysg.widget.dialog;

import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.viewpagerindicator.CirclePageIndicator;
import com.ysg.R;
import com.ysg.base.BaseDialog;
import com.ysg.http.data.entity.user.UserResult;
import com.ysg.utils.YCollectionUtil;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;
import com.ysg.widget.dialog.adapter.AccostPageAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 今日缘分:一键搭讪
 */
public class AccostDialog extends BaseDialog {
    public interface OnConfirmListener{
        void onConfirmClick(String ids, double sum);
    }
    private OnConfirmListener listener;
    public void setOnConfirmListener(OnConfirmListener listener){
        this.listener = listener;
    }

    private List<UserResult> list;
    public AccostDialog(Context context, List<UserResult> list) {
        super(context);
        for (UserResult item: list) {
            item.setChecked(true);
        }
        this.list = list;
    }

    private ImageView ivClose;
    private TextView tvConfirm;
    private ViewPager viewPager;
    private CirclePageIndicator circleIndicator;
    private AccostPageAdapter pageAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.dialog_accost;
    }

    public void initView() {
        ivClose = findViewById(R.id.ivClose);
        tvConfirm = findViewById(R.id.tvConfirm);
        viewPager = findViewById(R.id.viewPager);
        circleIndicator = findViewById(R.id.circleIndicator);

        pageAdapter = new AccostPageAdapter(getContext());
        pageAdapter.setData(YCollectionUtil.getPageList(list, 6));
        viewPager.setAdapter(pageAdapter);
        circleIndicator.setViewPager(viewPager);
    }


    @Override
    public void initData() {
    }

    @Override
    public void initListener() {
        tvConfirm.setOnClickListener((v)->{
            if (listener != null){
                List<String> ids = new ArrayList<>();
                double sum = 0;
                for (UserResult item: list) {
                    if (item.isChecked()){
                        ids.add(item.getId()+"");
                        try {
                            sum += Double.valueOf(item.getMessagePrice());
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }
                if (YCollectionUtil.isEmpty(ids)) {
                    YToastUtil.showShort(R.string.dialog_accost_no_tip);
                    return;
                }
                listener.onConfirmClick(YStringUtil.getIds(ids), sum);
            }
        });
        ivClose.setOnClickListener((v)->{
            dismiss();
        });
    }


}
